package com.wnxy.pawnbackgroundsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wnxy.pawnbackgroundsystem.entity.Channel;
import com.wnxy.pawnbackgroundsystem.dao.ChannelMapper;
import com.wnxy.pawnbackgroundsystem.exception.DemoRuntimeException;
import com.wnxy.pawnbackgroundsystem.dto.PageDto;
import com.wnxy.pawnbackgroundsystem.service.IChannelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 渠道商表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2021-10-14
 */
@Service
@Slf4j
public class ChannelServiceImpl extends ServiceImpl<ChannelMapper, Channel> implements IChannelService {
    @Resource
    private ChannelMapper channelMapper;
    //根据动态条件分页查询渠道商详情
    public IPage<Channel> findChannel(PageDto pageDto, Channel channel){
        log.info("开始执行根据名字分页查询渠道商详情: 参数= {},{}", pageDto,channel);
        Page<Channel> page = new Page<Channel>(pageDto.getPageIndex(), pageDto.getPageSize());
        QueryWrapper<Channel> qw = new QueryWrapper<>();
        qw.eq("channel_status_delect",channel.getChannelStatusDelect());
        if (null != channel) {
            if (null != channel.getChannelName()) {
                qw.like("channel_name", channel.getChannelName());
            }
            if (null != channel.getChannelPhone()) {
                qw.like("channel_phone", channel.getChannelPhone());
            }
            if (null != channel.getChannelStatus()) {
                qw.eq("channel_status", channel.getChannelStatus());
            }
            if (null != channel.getChannelNum()){
                qw.like("channel_num",channel.getChannelNum());
            }
            qw.orderByDesc(true,"channel_id");
        }
        IPage<Channel> channelIPage = channelMapper.selectPage(page, qw);
        return channelIPage;
    }
    //修改渠道商详情
    public Channel updateChannel(Channel channel){
        log.info("开始执行修改渠道商详情: 参数= {}",channel);
        Channel channel1 = channelMapper.selectById(channel.getChannelId());
        if (null == channel1) {
            throw new DemoRuntimeException("这个商品不存在");
        }
        int i = channelMapper.updateById(channel);
        if (i == 0) {
            throw new DemoRuntimeException("修改失败");
        }
        Channel channel2 = channelMapper.selectById(channel.getChannelId());
        return channel2;
    }
    //新增渠道商详情
    public Channel addChannel(Channel channel){
        log.info("开始执行新增渠道商详情: 参数= {}",channel);
        int insert = channelMapper.insert(channel);
        if (insert == 0) {
            throw new DemoRuntimeException("添加失败");
        }
        Channel channel1 = channelMapper.selectById(channel.getChannelId());
        if (null == channel1) {
            throw new DemoRuntimeException("4001","添加成功，返回结果为空");
        }
        return channel1;
    }

}
